#include <AtlasBase.h>
#include <AtlasCommon.h>
#include <AtlasServer.h>

#include "ExLobbyAvatarNode.h"
#include "ExLobbyCommon.h"

namespace Atlas
{
	_U32 CExLobbyAvatarNodeClient::ms_AvatarAlloc = 1;

	CExLobbyAvatarComponent::CExLobbyAvatarComponent(CExLobbyAvatarNodeServer* pServer) : CServerComponent(pServer), m_LobbyStub(this)
	{
		pServer->RegisterStub(GetServerStubID<::LOBBY_C2S>(),
		std::tr1::bind( &LOBBY_STUB::OnData, &m_LobbyStub, std::tr1::placeholders::_1, std::tr1::placeholders::_2, std::tr1::placeholders::_3, std::tr1::placeholders::_4)
		);
	}

	CExLobbyAvatarComponent::~CExLobbyAvatarComponent()
	{
	}

	void CExLobbyAvatarComponent::CreateAvatar(CExLobbyAvatarNodeClient* pClient, const char* name)
	{
		EXLOBBY_AVATAR& avatar = pClient->GetAvatar();
		if(avatar.aid != 0)
		{
			pClient->Lobby.CreateAvatarResult(RET_CODE_FAILED);
		}
		else
		{
			avatar.aid = CExLobbyAvatarNodeClient::NewAvatarID();
			avatar.name = name;
			pClient->Lobby.CreateAvatarResult(RET_CODE_SUCC);
		}
	}

	void CExLobbyAvatarComponent::Echo(CExLobbyAvatarNodeClient* pClient, const char* szEcho )
	{
		pClient->Lobby.Echo(szEcho);
	}

	void CExLobbyAvatarComponent::GetAvatar( CExLobbyAvatarNodeClient* pClient )
	{
		EXLOBBY_AVATAR& avatar = pClient->GetAvatar();
		if(avatar.aid == 0)
		{
			pClient->Lobby.AvatarList(NULL, 0);
		}
		else
		{
			pClient->Lobby.AvatarList(&avatar, 1);
		}
	}

	void CExLobbyAvatarComponent::SelectAvatar( CExLobbyAvatarNodeClient* pClient, _U32 aid )
	{
		EXLOBBY_AVATAR& avatar = pClient->GetAvatar();
		if(avatar.aid == 0)
		{
			pClient->Lobby.AvatarSelected(RET_CODE_NO_AVATAR);
		}
		else
		{
			pClient->Lobby.AvatarSelected(avatar.aid);
		}

	}

	void CExLobbyAvatarComponent::ListRoom(CExLobbyAvatarNodeClient* pClient, _U64 id)
	{
		m_GameMgr.ListRoom(pClient, id);
	}

	void CExLobbyAvatarComponent::CreateRoom(CExLobbyAvatarNodeClient* pClient, const char* name)
	{
		m_GameMgr.CreateRoom(pClient, name);
	}


	void CExLobbyAvatarComponent::EnterRoom( CExLobbyAvatarNodeClient* pClient, _U64 id )
	{
		m_GameMgr.EnterRoom(pClient, id);
	}

	void CExLobbyAvatarComponent::SendMessage( CExLobbyAvatarNodeClient* pClient, const char* szMsg )
	{
		m_GameMgr.SendMsg(pClient, szMsg);
	}

	void CExLobbyAvatarComponent::LeaveRoom( CExLobbyAvatarNodeClient* pClient )
	{
		m_GameMgr.LeaveRoom(pClient);
	}

	CExLobbyAvatarNodeServer::CExLobbyAvatarNodeServer(CServerApp* pServerApp) : CNodeServer(pServerApp, NODEID_AVATAR)
	{
		AddComponent(ATLAS_NEW CExLobbyAvatarComponent(this));
	}

	CExLobbyAvatarNodeServer::~CExLobbyAvatarNodeServer()
	{
	}

	CNodeClient* CExLobbyAvatarNodeServer::NewNodeClient(_U64 nndx, _U64 cndx, HSERVER hCluster)
	{
		return ATLAS_NEW CExLobbyAvatarNodeClient(this, nndx, cndx, hCluster);
	}

	CExLobbyAvatarNodeClient::CExLobbyAvatarNodeClient(CExLobbyAvatarNodeServer* pServer, _U64 nndx, _U64 cndx, HSERVER hCluster) : CNodeClient(pServer, nndx, cndx, hCluster), Lobby(this)
	{
		m_RoomID = ROOM_ID_INVAILD;
		m_Avatar.aid = 0;
		m_Avatar.name = "";
	}

	CExLobbyAvatarNodeClient::~CExLobbyAvatarNodeClient()
	{
	}

	void CExLobbyAvatarNodeClient::OnConnect(_U32 len, const _U8* data)
	{
		Lobby.LoginDone();
	}

	void CExLobbyAvatarNodeClient::OnDisconnect()
	{
		CNodeClient::OnDisconnect();
	}

	void CExLobbyAvatarNodeClient::OnData(_U16 code, _U32 len, const _U8* data)
	{
	}

	void CExLobbyAvatarNodeClient::OnEvent(_U16 code, _U32 len, const _U8* data)
	{
	}

	_U64 CExLobbyAvatarNodeClient::GetRoomID()
	{
		return m_RoomID;		
	}
	
	void CExLobbyAvatarNodeClient::SetRoomID(_U64 RoomID)
	{
		m_RoomID = RoomID;
	}

	EXLOBBY_AVATAR& CExLobbyAvatarNodeClient::GetAvatar()
	{
		return m_Avatar;
	}

	void CExLobbyAvatarNodeClient::SetAvatar(EXLOBBY_AVATAR& Avatar)
	{
		m_Avatar = Avatar;
	}
	
	_U32 CExLobbyAvatarNodeClient::NewAvatarID()
	{
		return ms_AvatarAlloc++;
	}
}
